package com.joesem.sale.client.logic.event

import android.app.Application
import com.joesem.sale.client.base.BaseViewModel
import com.joesem.sale.client.base.request
import com.joesem.sale.client.base.toast
import com.joesem.sale.client.network.data.*
import java.io.File


var eventParamsData: EventParamsMode? = null

class EventViewModel(application: Application) : BaseViewModel(application) {

    fun uploadImg(img: File) {
        request(isLoading = true, request = {
            uploadImg(img)
        }, responseFailed = {
            toast(it.message)
        }, response = { it ->
            it.data?.let {
                actionIntent(ImgUploadIntent(it))
            } ?: toast("上传失败 ${it.msg.orEmpty()}")
        })
    }

    fun getEventDetails(id: Int?) {
        request(request = { eventDetails(id) }, response = {
            it.data?.let {
                actionIntent(EventDetailsIntent(it))
            } ?: run {
                actionIntent(EventDetailsIntent(null))
                toast("暂无数据！")
            }
        }, responseFailed = {
            toast(it.message)
        })
    }

    fun eventAllot(id: Int?, userId: Int?) {
        request(isLoading = true, request = {
            eventAllot(id, hashMapOf("user_id" to userId))
        }, response = {
            it.data?.let {
                actionIntent(EventDetailsIntent(it))
            } ?: run {
                actionIntent(EventDetailsIntent(null))
                toast("暂无数据！")
            }
        }, responseFailed = {
            toast(it.message)
        })
    }

    fun confirmReturn(id: Int?, value: HashMap<String, Any?>) {
        request(isLoading = true, request = {
            eventReturn(id, value)
        }, response = {
            it.data?.let {
                actionIntent(EventDetailsIntent(it))
            } ?: run {
                actionIntent(EventDetailsIntent(null))
            }
        }, responseFailed = {
            toast(it.message)
        })
    }

    fun eventLis(params: EventFilterMultipleMode?, state: String?, isFirst: Boolean) {
        request(request = {
            eventList(state, params, isFirst)
        }, response = {
            it.data?.let {
                actionIntent(EventListIntent(it.filterNotNull(), isFirst))
            } ?: run {
                actionIntent(EventListIntent(null, isFirst))
            }
        }, responseFailed = {
            toast(it.message)
            actionIntent(EventListIntent(null, isFirst))
        })
    }

    fun getEventParams() {
        if (eventParamsData != null) {
            actionIntent(EventParamsIntent(eventParamsData!!))
        } else {
            request(isLoading = true, request = {
                getEventParams()
            }, response = {
                it.data?.let {
                    eventParamsData = it
                    actionIntent(EventParamsIntent(it))
                } ?: run {
                    toast("暂无筛选数据")
                }
            }, responseFailed = {
                toast(it.message)
            })
        }
    }

    class EventDetailsIntent(val data: EventDetailsMode?)
    class EventParamsIntent(val data: EventParamsMode)
    class EventCreateOrderIntent(val data: OrderCreateMode)
    class EventListIntent(val data: List<EventListMode>?, val isFirst: Boolean)
    class ImgUploadIntent(val mode: UploadImgMode)
}